package com.iengoo.servlet.ctrl;

import com.iengoo.servlet.entity.Ticket;
import com.iengoo.servlet.model.Page;
import com.iengoo.servlet.service.TicketService;
import com.iengoo.servlet.service.impl.TicketServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-01-29 11:36
 */
@WebServlet(urlPatterns = "/ticket/*")
public class StationCtrl extends HttpServlet {
    private TicketService service = new TicketServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String uri = req.getRequestURI();
        String page = req.getParameter("page"); // 获取页码参数
        String size = req.getParameter("size"); // 获取每页显示数量

        int p = Integer.parseInt(page);  // 字符串数字转换成数字
        int s = Integer.parseInt(size);

        Page pageObj = new Page();
        pageObj.setPage(p);
        pageObj.setSize(s);

        if ("/ticket/query".equals(uri)) {
            List<Ticket> ticketList = service.query(pageObj);
            req.setAttribute("ticketList", ticketList);
            // 返回页面到浏览器端
            req.getRequestDispatcher("/WEB-INF/jsp/ticketList.jsp")
                    .forward(req, resp);
        }
    }
}